package com.ldy.biz.shop.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ldy.biz.shop.entity.ShopInfo;
import com.ldy.biz.shop.service.ShopInfoService;
import com.ldy.core.common.Result;
import com.ldy.core.service.BaseOrgService;
import com.ldy.core.util.MySession;

import cn.hutool.core.collection.CollectionUtil;

/**
 * 店家动态管理
 */
@RestController
@RequestMapping("/shopinfo")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class ShopInfoController {

	private static final Logger logger = LoggerFactory.getLogger(ShopInfoController.class);

	@Autowired
	private ShopInfoService shopInfoService;
	
	
	@Autowired
	private BaseOrgService orgService;


	/**
	 * 列表分页查询
	 * 
	 * @param current
	 * @param size
	 * @param title
	 * @return
	 */
	@PostMapping("/selectPage")
	public Result selectPage(int current, int size, String title) {
		Page<ShopInfo> page = new Page<ShopInfo>(current, size);
		QueryWrapper<ShopInfo> query = new QueryWrapper<>();
		if (StringUtils.hasText(title)) {
			query.like("title", title);
		}
		List<String> orgs = orgService.getMySubOrgs();
		if (CollectionUtil.isNotEmpty(orgs)) {
			query.in("org_code", orgs);
		}
		query.orderByDesc("issue_time, title");
		return Result.success(shopInfoService.page(page, query));
	}

	/**
	 * 列表查询
	 * 
	 * @return
	 */
	@PostMapping("/selectList")
	public Result selectList(ShopInfo vo) {
		QueryWrapper<ShopInfo> query = new QueryWrapper<>();
		if (StringUtils.hasText(vo.getOrgCode())) {
			query.eq("org_code", vo.getOrgCode());
		} else {
			// 查询和我有关系
			List<String> orgs = orgService.getMySubOrgs();
			if (CollectionUtil.isNotEmpty(orgs)) {
				query.in("org_code", orgs);
			}
		}
		query.orderByDesc("issue_time");
		return Result.success(shopInfoService.list(query));
	}

	/**
	 * 新增
	 * 
	 * @return
	 */
	@PostMapping("/insert")
	public Result insert(ShopInfo param) {
		logger.info("method: {}, params:[param: {}]", "insert", JSONObject.toJSONString(param));
		param.setOrgCode(MySession.get().getOrgCode());
		shopInfoService.save(param);
		return Result.success(param);
	}
	
	/**
	 * 排序
	 * 
	 * @return
	 */
	@PostMapping("/sort")
	public Result sort(@RequestBody List<ShopInfo> param) {
		logger.info("method: {}, params:[param: {}]", "sort", JSONObject.toJSONString(param));
		shopInfoService.saveOrUpdateBatch(param);
		return Result.success();
	}

	/**
	 * 更新
	 * 
	 * @return
	 */
	@PostMapping("/update")
	public Result update(ShopInfo param) {
		logger.info("method: {}, params:[app: {}]", "update", JSONObject.toJSONString(param));
		shopInfoService.updateById(param);
		return Result.success();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@PostMapping("/delete")
	public Result delete(String id) {
		logger.info("method: {}, params:[id: {}]", "delete", id);
		shopInfoService.removeById(id);
		return Result.success();
	}
}
